//
//  GuideRecordViewController.m
//  SHHCPhone
//
//  Created by sheng long on 14-7-1.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "GuideRecordViewController.h"
#import "BodyIngredientCell.h"

@interface GuideRecordViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *mainTable;
    
    BOOL isFirst;
}
@property (nonatomic,retain) NSArray *dataArray;

@end

@implementation GuideRecordViewController

@synthesize dataArray;
@synthesize weight;

- (void)viewDidLoad
{
    [super viewDidLoad];

    navigationBarView.title = @"简单指导结果";
    [navigationBarView setNormalLeftButton];
    
    [self setDataArray:nil];
}

-(void)doneButt
{
    [self.navigationController pushViewController:[[AppDelegate getAppDelegate] getHomeVC] animated:YES];
}

-(void)showView
{
    mainTable = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [mainTable setDelegate:self];
    [mainTable setDataSource:self];
    [mainTable setSeparatorColor:[UIColor clearColor]];
    [mainTable setBackgroundColor:[UIColor clearColor]];
    [mainTable setBounces:NO];
    [mainTable setTableHeaderView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, mainTable.frame.size.width, 25)]];
    [mainTable setTableFooterView:[self setFooterView]];
    [self.view addSubview:mainTable];
}

-(UIView*)setFooterView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, mainTable.frame.size.width, 144)];
    [view setBackgroundColor:[UIColor clearColor]];
    
    UIButton *doneButton = [OftenMenthod setButtonTitle:@"进入首页"];
    [self.view addSubview:doneButton];
    [doneButton addTarget:self action:@selector(doneButt) forControlEvents:UIControlEventTouchUpInside];
    [doneButton setCenter:CGPointMake(view.frame.size.width/2,10+doneButton.frame.size.height/2)];
    [view addSubview:doneButton];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.textColor = GET_COLOR(@"b0b0b0");
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:14];
    label.text = @"提示：";
    [label sizeToFit];
    [label setCenter:CGPointMake(20+label.frame.size.width/2, doneButton.frame.size.height+doneButton.frame.origin.y+12+label.frame.size.height/2)];
    [view addSubview:label];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(label.frame.origin.x, label.frame.origin.y+label.frame.size.height+14, view.frame.size.width-label.frame.origin.x*2, 44)];
    bgView.backgroundColor = GET_COLOR(@"fbfbfb");
    bgView.layer.borderWidth = .5;
    bgView.layer.borderColor = GET_COLOR(@"dddddd").CGColor;
    bgView.layer.cornerRadius = 2;
    [view addSubview:bgView];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectZero];
    label1.textColor = GET_COLOR(@"8d8d8d");
    label1.font = [UIFont systemFontOfSize:14];
    label1.backgroundColor = [UIColor clearColor];
    label1.text = @"如检测体成分，可获得个性化的膳食指导。";
    [label1 sizeToFit];
    [label1 setCenter:CGPointMake(bgView.frame.size.width/2,bgView.frame.size.height/2)];
    [bgView addSubview:label1];
    
    return view;
}

-(void)viewWillAppear:(BOOL)animated
{
    if (!isFirst) {
        isFirst = YES;
        [self againLoading];
    }
}

//重新加载数据
-(void)againLoading
{
    [self showProgressView:nil belowSubview:navigationBarView];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:IOS_GUIDE forKey:IOS_METHOD_NAME];
    [dic setValue:APP_USER_MSG.id forKey:@"user_id"];
    if (weight) {
        [dic setValue:weight forKey:@"weight"];
    }
    
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_GUIDE_TAG need:NO];
    [self.netArray addObject:request];
    [request startAsynchronous];
}


#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%d",indexPath.section];
    BodyIngredientCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        
        cell = [[BodyIngredientCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier size:[tableView rectForRowAtIndexPath:indexPath].size];
        [cell setBackgroundColor:[UIColor clearColor]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    [cell setContent:[dataArray objectAtIndex:indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 52;
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@",[request responseString]);
    NSDictionary *dic = [[request responseString] objectFromJSONString];
    if (dic != nil) {
        CLog(@"dic==%@",dic);
        
        NSArray *array1 = @[@"体成分-体重icon",@"体成分-体脂率icon",@"体成分-肌肉率icon",@"体成分-水分icon"];
        NSArray *array2 = @[@"bmi",@"fat_percentage",@"basal_metabolism",@"energy"];
        NSArray *array3 = @[@"体质指数BMI:      ",@"身体肥胖度:      ",@"基础代谢率:      ",@"每日需要能量:      "];
        NSArray *array4 = @[[NSString stringWithFormat:@"%@/m²",APP_WEIGHT_UNIT],@"",@"kcal/d",@"kcal"];
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
        for (int i = 0; i < array1.count; i++) {
            BodyIngredientInfo *info = [[BodyIngredientInfo alloc] init];
            info.headImageName = [array1 objectAtIndex:i];
            info.title = [NSString stringWithFormat:@"%@%@%@",[array3 objectAtIndex:i],[dic valueForKey:[array2 objectAtIndex:i]],[array4 objectAtIndex:i]];
            [array addObject:info];
            if (i == 0 ) {
                info.Loc = 1;
            }else if(i == array1.count-1){
                info.Loc = 2;
            }
        }
        
        dataArray = [[NSArray alloc] initWithArray:array];
        
        [self hideProgressView];
        [self showView];
    }else{
        [self requestFailed:request];
    }
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    [self clickAgainLoadingView:0];

}


@end
